package in.muton.edushare.pages.batch;

import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.curricula.CurriculaSummary;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.vo.Batches;

public class BatchGradeBase extends EduShareBasePage {

	private IModel<Batches> batchModel;

	public BatchGradeBase(PageParameters params) {
		getBatch(params);
		init();
	}

	public BatchGradeBase(IModel<Batches> batchModel) {
		this.batchModel = batchModel;
		init();
	}

	private void init() {
		add(new Label("batchName", batchModel.getObject().getBatchName()));
		PageParameters pageParams = new PageParameters();
		pageParams.put("0", Integer.toString(batchModel.getObject()
				.getCurricula().getCurriculaId()));
		Link<Void> l = null;
		add(l = new BookmarkablePageLink<Void>("currLink",
				CurriculaSummary.class, pageParams));
		l.add(new Label("currName", batchModel.getObject().getCurricula()
				.getCurriculaName()));
	}

	private void getBatch(PageParameters params) {
		int id = params.getAsInteger("0", 0);
		Batches b = new BatchDAO(getSessionORMInterface()).getBatchesById(id);
		if (b == null) {
			throwPageNotFoundException();
		}
		DeptRoleCheck drc = new DeptRoleCheck(b.getCurricula().getDepartment(),
				getLoggedInUser());
		if (!(drc.isAdmin() || drc.isTeacher())) {
			throwUnauthorisedException();
		}
		this.batchModel = new CayenneModel<Batches>(b);
	}

	@Override
	public String getPageTitle() {
		return batchModel.getObject().getBatchName() + " : Edit Grades";
	}

	@Override
	public String getUserRole() {
		return "";
	}

	public IModel<Batches> getBatchModel() {
		return batchModel;
	}

	public void setBatchModel(IModel<Batches> batchModel) {
		this.batchModel = batchModel;
	}
}
